using System.Net.Sockets;
using System.Numerics;
using System.Runtime.InteropServices;
using System.Timers;

namespace MyDesktopPetDemo
{
    public partial class FormFoodGame : Form
    {
        FoodGame foodGame = new();

        public FormFoodGame()
        {
            InitializeComponent();
        }

        public FormFoodGame(FoodGame foodGame) : this()
        {
            this.foodGame = foodGame;
        }

        private void FormFoodGame_Load(object sender, EventArgs e)
        {
            foodGame.Timer.Elapsed += Update;

            foodGame.Start();
        }

        private void Update(object? sender, ElapsedEventArgs e)
        {
            try
            {
                pic.Invoke(new Action(() =>
                {
                    foodGame.CatPosX = (float)(Cursor.Position.X - pic.PointToScreen(new Point(0, 0)).X) / pic.Width;
                    pic.Invalidate();
                }));
            }
            catch (Exception) { }
        }

        private void pic_Paint(object sender, PaintEventArgs e)
        {
            foodGame.Render(e.Graphics, pic);
        }

        private void pic_MouseMove(object sender, MouseEventArgs e)
        {
            foodGame.CatPosX = (float)e.X / pic.Width;
        }

        private void pic_MouseEnter(object sender, EventArgs e)
        {
            Cursor.Hide();
        }

        private void pic_MouseLeave(object sender, EventArgs e)
        {
            Cursor.Show();
        }
    }
}